package top.callname.util;

import org.apache.log4j.Logger;
import org.weixin4j.model.base.Token;


/** 
* 定时获取微信access_token的线程 
*/  
public class TokenThread implements Runnable {  

	 private final  Logger logger = Logger.getLogger(TokenThread.class); 
     public static Token accessToken = null;  
 
	   public void run() {  
	       while (true) {  
	           try {  
	        	   accessToken = WeixinUtil.getAccessToken();  
	               if (null != accessToken) {  
	            	   logger.info("获取access_token成功：有效时常"+ accessToken.getExpires_in());  
	                   // 休眠7000秒  
	                   Thread.sleep((accessToken.getExpires_in() - 200) * 1000);  
	               } else {  
	                   // 如果access_token为null，60秒后再获取  
	                   Thread.sleep(60 * 1000);  
	               }  
	           } catch (InterruptedException e) {  
	               try {  
	                   Thread.sleep(60 * 1000);  
	               } catch (InterruptedException e1) {  
	            	   logger.error("{}", e1);  
	               }  
	               logger.error("{}", e);  
	           }  
	       }  
	   }  
}  